package com.sinodata.bsm.cicp.util.pool;

import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.log4j.Logger;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-12 PM 3:46:06    wangpeng       1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class URLPoolableObject implements PoolableObjectFactory {
    private static Logger logger = Logger.getLogger("urllog");
    private String urlStr;

    public URLPoolableObject(String urlStr) {
        this.urlStr = urlStr;
    }

    @Override
    public void activateObject(Object arg0) throws Exception {

    }

    @Override
    public void destroyObject(Object arg0) throws Exception {
        logger.info("销毁了一个url connection");
        if (arg0 != null && arg0 instanceof HttpURLConnection) {
            HttpURLConnection connection = (HttpURLConnection) arg0;
            connection.disconnect();
        }
    }

    @Override
    public Object makeObject() throws Exception {
        logger.info("创建了一个新的连接");
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        return connection;
    }

    @Override
    public void passivateObject(Object arg0) throws Exception {

    }

    @Override
    public boolean validateObject(Object arg0) {
        return false;
    }

    public String getUrlStr() {
        return urlStr;
    }

    public void setUrlStr(String urlStr) {
        this.urlStr = urlStr;
    }

}
